package com.mxpio.erp.technology.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.technology.entity.WorkShop;
import com.mxpio.erp.common.technology.service.WorkShopService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "WorkShopController", description = "车间数据接口")
@RestController
@RequestMapping("/erp/tech/workshop/")
public class WorkShopController {
	@Autowired
	private WorkShopService workShopSerivce;

	@GetMapping("list")
	@Operation(summary = "车间列表", description = "获取车间列表", method = "GET")
	public Result<List<WorkShop>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<WorkShop> items = workShopSerivce.list(WorkShop.class, criteria);
		return Result.OK(items);
	}
	
	@GetMapping("page")
	@Operation(summary = "车间列表", description = "获取车间列表", method = "GET")
	public Result<Page<WorkShop>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo-1, pageSize);
		Page<WorkShop> items = workShopSerivce.listPage(WorkShop.class, page, criteria);
		return Result.OK(items);
	}
	
	@GetMapping("list/{workShopCode}")
	@Operation(summary = "根据workShopCode获取车间", description = "根据workShopCode获取车间", method = "GET")
	public Result<WorkShop> getById(@PathVariable(name = "workShopCode", required = true) String workShopCode) {
		WorkShop item = workShopSerivce.getById(WorkShop.class, workShopCode);
		return Result.OK(item);
	}
	
	@PostMapping("add")
	@Operation(summary = "新增车间", description = "新增车间", method = "POST")
	public Result<WorkShop> add(@RequestBody WorkShop workShop) {
		workShopSerivce.save(workShop);
		return Result.OK(workShop);
	}
	
	@PutMapping("edit")
	@Operation(summary = "编辑车间", description = "编辑车间（全量）", method = "PUT")
	public Result<WorkShop> edit(@RequestBody WorkShop workShop) {
		workShopSerivce.update(workShop);
		return Result.OK(workShop);
	}
	
	@DeleteMapping("remove/{workShopCode}")
	@Operation(summary = "删除车间", description = "删除车间", method = "DELETE")
	public Result<Item> remove(@PathVariable(name = "workShopCode", required = true) String workShopCode) {
		String workShopCodes[] = workShopCode.split(",");
		for(String key : workShopCodes){
			workShopSerivce.delete(WorkShop.class, key);
		}
		return Result.OK();
	}
}
